//storage.js
// First, let's create a short helper function to get the database connection
function getDatabase() {
     return openDatabaseSync("qiushi", "1.0", "StorageDatabase", 100000);
}

// At the start of the application, we can initialize the tables we need if they haven't been created yet
function initialize() {
    var db = getDatabase();
    db.transaction(
        function(tx) {
            // Create the settings table if it doesn't already exist
            // If the table exists, this is skipped
            tx.executeSql('CREATE TABLE IF NOT EXISTS settings(setting TEXT UNIQUE, value TEXT)');
            tx.executeSql('CREATE TABLE IF NOT EXISTS userCache(item TEXT UNIQUE, value TEXT)')
      });
}
function setCache(item, value) {
   var db = getDatabase();
   var res = "";
   db.transaction(function(tx) {
        var rs = tx.executeSql('INSERT OR REPLACE INTO userCache VALUES (?,?);', [item, value]);
              if (rs.rowsAffected > 0) {
                res = "OK";
              } else {
                res = "Error";
              }
        }
  );
  return res;
}
function getCache(item) {
   var db = getDatabase();
   var res="";
   db.readTransaction(function(tx) {
     var rs = tx.executeSql('SELECT value FROM userCache WHERE item=?;', [item]);
     if (rs.rows.length > 0) {
          res = rs.rows.item(0).value;
     } else {
         res = "";
     }
  })
  return res
}
function clearCache(item){
    var db = getDatabase();
    db.transaction(function(tx) {
                       tx.executeSql('DELETE FROM userCache WHERE item=?;', [item])
                   });
}
// This function is used to write a setting into the database
function setSetting(setting, value) {
   // setting: string representing the setting name (eg: “username”)
   // value: string representing the value of the setting (eg: “myUsername”)
   var db = getDatabase();
   var res = "";
   db.transaction(function(tx) {
        var rs = tx.executeSql('INSERT OR REPLACE INTO settings VALUES (?,?);', [setting,value]);
              //console.log(rs.rowsAffected)
              if (rs.rowsAffected > 0) {

                 // console.log("cun shu ju :"+setting+"==="+value);
                  if(setting==="contentcolor" ){
                      defcolor = value;
                      appWindow.contntcolor = defcolor
                  }
                   if(setting==="labelcolor"){
                      labelcolor = value
                      appWindow.labelcolor = labelcolor
                  }

                  if(setting==="showimage"){
                      showimage = value
                      appWindow.showimage =showimage
                  }
                  if(setting==="fontsize"){
                      appWindow.fontsize = value
                  }
                  if(setting==="systemcolor"){
                      appWindow.color = value
                  }
                  if(setting==="lockposition"){
                      appWindow.lockposition=value;
                  }

                res = "OK";
              } else {
                res = "Error";
              }
        }
  );
  // The function returns “OK” if it was successful, or “Error” if it wasn't
  return res;
}
// This function is used to retrieve a setting from the database
function getSetting(setting) {
   var db = getDatabase();
   var res="";
   db.transaction(function(tx) {
     var rs = tx.executeSql('SELECT value FROM settings WHERE setting=?;', [setting]);
     if (rs.rows.length > 0) {
          res = rs.rows.item(0).value;

     } else {
         res = "Unknown";
     }
   //  console.log("去数据啦："+setting+"========"+res)
  })
  // The function returns “Unknown” if the setting was not found in the database
  // For more advanced projects, this should probably be handled through error codes
  return res
}
var defcolor = "";
function getContentColor(){
    if(defcolor===""){
    var b = getSetting("contentcolor");
        defcolor=  b==="Unknown"?"black":b;
    }
    return defcolor;
}
var labelcolor = "";
function getlabelcolor(){

    if(labelcolor===""){
       var b = getSetting("labelcolor");

       b= b==="Unknown"?getContentColor():b;
        // console.log("b2:=============="+b);
       b= b==="Unknown"?"black":b;

       labelcolor = b;
    }
    return labelcolor
}
var showimage;
function getshowimage(){
    if(null===showimage||undefined===showimage){
        var b = getSetting("showimage");
        if(b==="Unknown"||b==="true"||b===true){
            showimage = true;
        }
        else{
            showimage = false
        }
    }
    return showimage
}

function callbackFunction(which,whichcolor){
    //text1.text  =  colorList.get(colorColumn.selectedIndex).value

    Store.initialize();
    Store.setSetting(which,whichcolor );
    if(which==="labelcolor"){
       text41.color = whichcolor;
    }
    if(which==="contentcolor"){
       text1.color = whichcolor;
    }
    if(which==="fontsize"){
        text1.font.pointSize = whichcolor;
    }
}
